from time import sleep
from random import randint
from selenium import webdriver
from selenium.webdriver.support.select import Select
import unittest
from HTMLTestRunner import HTMLTestRunner



class JXC_big_test(unittest.TestCase):

    # 在每个测试方法执行前，自动调用本方法
    def setUp(self):
        self.driver = webdriver.Firefox()
        # 打开登录页面
        self.driver.get('http://localhost/JXC/index.asp')


    # 在每个测试方法执行后，自动调用本方法
    def tearDown(self):
        self.driver.quit()


    def test_login_error(self):
         # 输入错误的用户名和密码
        self.driver.find_element_by_name('username').send_keys('abcd')
        self.driver.find_element_by_name('pwd').send_keys('12234')
        self.driver.find_element_by_name('enter').click()

        # 获取提示框中的提示文字
        alert = self.driver.switch_to.alert.text
        # 断言：应该弹出错误的提示信息
        # 提示信息中应该出现“。。。。密码错误!”这几个字
        self.assertIn('密码错误', alert)


    def test_login_ok(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()

        # 断言：登录是否成功
        #  依据：'后台管理系统'这几个字，出现在页面源代码中
        self.assertIn('后台管理系统', self.driver.page_source)

    def test_delunit(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        module_xpath = '/html/body/table/tbody/tr[13]'
        menu_xpath = '/html/body/table/tbody/tr[14]/td/table/tbody/tr[7]/td'
        self.driver.find_element_by_xpath(module_xpath).click()
        self.driver.find_element_by_xpath(menu_xpath).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(1)

        self.driver.find_element_by_id('chkall').click()
        bt_del_css_path = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(bt_del_css_path).click()
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertNotIn("name='ID'", self.driver.page_source)



    def test_addunit(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        sleep(2)
        # 从当前默认位置切换到菜单框架中
        self.driver.switch_to.frame('left')

        # 点击菜单 系统设置
        sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(sysset_css).click()

        sleep(2)
        # 继续点击：计量单位管理
        danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(danwei_css).click()
        sleep(1)

        # 切换原始位置
        self.driver.switch_to.default_content()
        # 切到右侧框架
        self.driver.switch_to.frame('right')
        sleep(2)
        # 点击添加单位按钮
        bt_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(bt_css).click()
        sleep(2)

        # 输入单位名称
        self.driver.find_element_by_name('danwei').send_keys('哈哈哈')
        # 点击确认按钮
        self.driver.find_element_by_name('submit').click()
        sleep(2)
        # 处理弹框
        self.driver.switch_to.alert.accept()
        # # 切换原始位置
        # driver.switch_to.default_content()
        sleep(2)

        self.assertIn('哈哈哈',self.driver.page_source)

    def test_adddalei(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        # 切换原始位置
        self.driver.switch_to.default_content()
        # 从当前默认位置切换到菜单框架中
        self.driver.switch_to.frame('left')

        # 点击菜单 系统设置
        sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(sysset_css).click()
        sleep(2)
        # 继续点击：产品大类管理
        dalei_css = '#g_6 > table > tbody > tr:nth-child(5) > td > table > tbody > tr > td:nth-child(2)'
        self.driver.find_element_by_css_selector(dalei_css).click()
        sleep(1)

        # 切换原始位置
        self.driver.switch_to.default_content()
        # 切到右侧框架
        self.driver.switch_to.frame('right')
        sleep(1)
        # 点击添加大类按钮
        dlbt_css = 'body > table > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td:nth-child(2) > input'
        self.driver.find_element_by_css_selector(dlbt_css).click()
        sleep(2)
        #添加一个大类
        self.driver.find_element_by_name('bigclass').send_keys('手机')
        sleep(1)
        #点击确认
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        # 处理弹框
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('手机',self.driver.page_source)



    def test_addxiaolei(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        # 切换原始位置
        self.driver.switch_to.default_content()
        # 从当前默认位置切换到菜单框架中
        self.driver.switch_to.frame('left')
        # 点击菜单 系统设置
        sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(sysset_css).click()
        sleep(1)
        # 继续点击：产品小类管理
        xiaolei_css = '#g_6 > table > tbody > tr:nth-child(6) > td > table > tbody > tr > td:nth-child(2)'
        self.driver.find_element_by_css_selector(xiaolei_css).click()
        sleep(1)
        # 切换原始位置
        self.driver.switch_to.default_content()
        # 切到右侧框架
        self.driver.switch_to.frame('right')
        sleep(1)

        #点击添加小类按钮
        xlbt_css = 'body > table:nth-child(2) > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td:nth-child(2) > input'
        self.driver.find_element_by_css_selector(xlbt_css).click()
        sleep(2)
        # 获取列表项的数量
        lstnum=Select(self.driver.find_element_by_name('bigclass'))
        lst_count = len(lstnum.options)

        # 根据数量，生成一个随机数, 参数:闭区间的范围
        # 列表项数量：3， 序号范围：[0,2]
        # 随机生成一个列表项的序号[0,2]
        rand = randint(0, lst_count-1)

        # 通过随机产生的序号，选择一个列表项
        lstnum.select_by_index(rand)
        sleep(1)
        #添加一个小类
        self.driver.find_element_by_name('smallclass').send_keys('华为')
        sleep(1)
        #点击确认
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        # 处理弹框
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('手机', self.driver.page_source)
        # 切换到顶部框架
        self.driver.switch_to.frame('topFrame')
        self.driver.find_element_by_link_text('安全退出').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        sleep(2)






    # 测试集合（套）：专门指定用例执行的顺序
def test_suite():
        # 定义一个测试集合
        suite = unittest.TestSuite()
        # 向测试集合中添加要执行的测试方法
        #  规范：类名("方法名")
        suite.addTest(JXC_big_test('test_login_error'))
        suite.addTest(JXC_big_test('test_login_ok'))
        suite.addTest(JXC_big_test('test_delunit'))
        suite.addTest(JXC_big_test('test_addunit'))
        suite.addTest(JXC_big_test('test_adddalei'))
        suite.addTest(JXC_big_test('test_addxiaolei'))
        return suite

# 指明一个测试报告文件 - HTML格式文件
with open('d:/进销存系统测试报告.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='进销存系统测试报告')
    runner.run(test_suite())


# # 关闭浏览器
# driver.quit()
#
#
#


